package org.foreveross.que.infra.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.foreveross.que.infra.JobTransTaluoRepository;
import org.foreveross.que.infra.base.constant.SysConstants;
import org.foreveross.taluo.entity.AcUserEntity;
import org.openkoala.koala.auth.core.domain.Role;
import org.openkoala.koala.auth.core.domain.RoleUserAuthorization;
import org.openkoala.koala.auth.core.domain.User;
import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.utils.DateUtils;

@Service
public class JobTransTaluoRepositoryImpl implements JobTransTaluoRepository {

	@Transactional
	public boolean transUsers(List<AcUserEntity> acUsers) throws Exception{
		if(acUsers==null||acUsers.size()==0){
			return false;
		}
//		baseRepository.execute("delete from User");
//		baseRepository.execute("delete from Role");
		Map <String,User>oldUsers = this.getMapByList(User.findAll(User.class), "userAccount");
		
//		List<User> saveUsers = new ArrayList<User>();
		Date currDate = new Date();
		Integer i =1;
		for(AcUserEntity acUser : acUsers){
			RoleUserAuthorization r = null;
//			baseRepository.get(User.class, acUser.getIds()+"");
			User user = new User();
			if(oldUsers.containsKey(acUser.getUsername())){
				User olduser = oldUsers.get(acUser.getUsername());
				user = olduser;
				if(StringUtils.isBlank(olduser.getUserDesc())){
					user.setUserDesc("系统传递");
				}
			}else{
				user.setCreateDate(currDate);
				r = new RoleUserAuthorization();
				r.setAbolishDate(DateUtils.MAX_DATE);
				r.setCreateDate(currDate);
				Role role = new Role();
				role.setId(3L);
				r.setRole(role);
				r.setUser(user);
				r.setVersion(0);
				MessageDigestPasswordEncoder encoder = new MessageDigestPasswordEncoder("MD5");
				String pass = encoder.encodePassword(SysConstants.DEFAULT_PASS, acUser.getUsername());
				user.setUserPassword(pass);
				user.setLastLoginTime(DateUtils.MAX_DATE);
			}
			
			user.setCreateOwner(acUser.getCreatorusername());
			user.setAbolishDate(DateUtils.MAX_DATE);
			user.setLastModifyTime(currDate);
			user.setName(acUser.getUsername());
			user.setSerialNumber("0");
			user.setSortOrder(i++);
			user.setUserAccount(acUser.getUsername());
			user.setValid(true);
			user.setVersion(0);
			user.save();
			if(r!=null){
				r.save();
			}
		}
		return false;
//		user.setAuthorizations(authorizations);
//		user.setCreateDate(currDate);
//		user.setCreateOwner("admin");
//		user.setLastLoginTime(null)
	}
	
	
	/**
	 * 根據屬性名裝置map
	 * @ author liyongqiang
	 *
	 * @param list
	 * @param fieldName
	 * @return
	 * @throws Exception
	 */
	public static Map getMapByList(List list,String fieldName) throws Exception{
		Map resultMap = new HashMap();
		if(list!=null){
			for(int i =0 ;i<list.size();i++){
			    Object bean = list.get(i);
				Object value = BeanUtils.getProperty(bean, fieldName);
				resultMap.put(value, bean);
			}
		}
		return resultMap;
	}
	
}
